<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      go/4-Go语言框架/4-1-gin框架/2-gin路由 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>go/4-Go语言框架/4-1-gin框架/2-gin路由</h2>



  <p class="post-date">2020-09-15</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="一-基本路由"><a href="#一-基本路由" class="headerlink" title="一 基本路由"></a>一 基本路由</h1><ul>
<li>gin 框架中采用的路由库是基于httprouter做的</li>
<li>地址为：<a href="https://github.com/julienschmidt/httprouter" target="_blank" rel="noopener">https://github.com/julienschmidt/httprouter</a></li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">funcPost</span><span class="params">(c *gin.Context)</span></span>  &#123;</span><br><span class="line">	c.String(http.StatusOK, <span class="string">"post请求"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	r := gin.Default()</span><br><span class="line">	r.GET(<span class="string">"/"</span>, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">		c.String(http.StatusOK, <span class="string">"hello word"</span>)</span><br><span class="line">	&#125;)</span><br><span class="line">	r.POST(<span class="string">"/"</span>,funcPost)</span><br><span class="line">	<span class="comment">//r.DELETE()</span></span><br><span class="line">	<span class="comment">//r.PUT()</span></span><br><span class="line">	<span class="comment">//r.OPTIONS()</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//监听端口默认为8080</span></span><br><span class="line">	r.Run(<span class="string">":8000"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="二-API参数"><a href="#二-API参数" class="headerlink" title="二 API参数"></a>二 API参数</h1><ul>
<li>可以通过Context的Param方法来获取API参数</li>
<li>localhost:8000/xxx/zhangsan</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">	<span class="string">"strings"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	r := gin.Default()</span><br><span class="line">	r.GET(<span class="string">"/user/:name/*action"</span>, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">		name := c.Param(<span class="string">"name"</span>)</span><br><span class="line">		action := c.Param(<span class="string">"action"</span>)</span><br><span class="line">		fmt.Println(action)</span><br><span class="line">		<span class="comment">//把字符串开头/截取掉</span></span><br><span class="line">		action = strings.Trim(action, <span class="string">"/"</span>)</span><br><span class="line">		fmt.Println(action)</span><br><span class="line">		c.String(http.StatusOK, name+<span class="string">" is "</span>+action)</span><br><span class="line">	&#125;)</span><br><span class="line">	<span class="comment">//默认为监听8080端口</span></span><br><span class="line">	r.Run(<span class="string">":8000"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1giriqrxsu0j30yi09ajs7.jpg" alt="image-20200915183717884"></p>
<h1 id="三-URL参数"><a href="#三-URL参数" class="headerlink" title="三 URL参数"></a>三 URL参数</h1><ul>
<li>URL参数可以通过DefaultQuery()或Query()方法获取</li>
<li>DefaultQuery()若参数不存在，返回默认值，Query()若不存在，返回空串</li>
<li>API <a href="http://localhost:8080/user?name=lqz" target="_blank" rel="noopener">http://localhost:8080/user?name=lqz</a></li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line"></span><br><span class="line">	<span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	r := gin.Default()</span><br><span class="line">	r.GET(<span class="string">"/user"</span>, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">		<span class="comment">//指定默认值</span></span><br><span class="line">		<span class="comment">//http://localhost:8080/user 才会打印出来默认的值</span></span><br><span class="line">		<span class="comment">//name := c.DefaultQuery("name", "世界")</span></span><br><span class="line">		name := c.Query(<span class="string">"name"</span>)</span><br><span class="line">		c.String(http.StatusOK, fmt.Sprintf(<span class="string">"hello %s"</span>, name))</span><br><span class="line">	&#125;)</span><br><span class="line">	r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>不传递参数输出的结果：</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girius41c8j30k606wdg9.jpg" alt="image-20200915184112118"></p>
<p>传递参数输出的结果：</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girivcbiloj30n205m0t7.jpg" alt="image-20200915184144289"></p>
<h1 id="四-表单参数"><a href="#四-表单参数" class="headerlink" title="四 表单参数"></a>四 表单参数</h1><ul>
<li>表单传输为post请求，http常见的传输格式为四种：<ul>
<li>application/json</li>
<li>application/x-www-form-urlencoded </li>
<li>application/xml</li>
<li>multipart/form-data </li>
</ul>
</li>
<li>表单参数可以通过PostForm()方法获取，该方法默认解析的是x-www-form-urlencoded或from-data格式的参数</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line"></span><br><span class="line">	<span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	r := gin.Default()</span><br><span class="line">	r.POST(<span class="string">"/form"</span>, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">		username := c.PostForm(<span class="string">"username"</span>)</span><br><span class="line">		password := c.PostForm(<span class="string">"password"</span>)</span><br><span class="line">		<span class="comment">// c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))</span></span><br><span class="line">		c.String(http.StatusOK, fmt.Sprintf(<span class="string">"用户名:%s,密码:%s"</span>, username, password))</span><br><span class="line">	&#125;)</span><br><span class="line">	r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girkdzl4g4j318o0to77o.jpg" alt="image-20200915193412744"></p>
<h1 id="五-上传单个文件"><a href="#五-上传单个文件" class="headerlink" title="五 上传单个文件"></a>五 上传单个文件</h1><ul>
<li>multipart/form-data格式用于文件上传</li>
<li>gin文件上传与原生的net/http方法类似，不同在于gin把原生的request封装到c.Request中</li>
</ul>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	r := gin.Default()</span><br><span class="line">	<span class="comment">//限制上传最大尺寸</span></span><br><span class="line">	r.MaxMultipartMemory = <span class="number">8</span> &lt;&lt; <span class="number">20</span></span><br><span class="line">	r.POST(<span class="string">"/upload"</span>, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">		file, err := c.FormFile(<span class="string">"file"</span>)</span><br><span class="line">		<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">			c.String(<span class="number">500</span>, <span class="string">"上传图片出错"</span>)</span><br><span class="line">		&#125;</span><br><span class="line">		c.SaveUploadedFile(file, file.Filename)</span><br><span class="line">		c.String(http.StatusOK, file.Filename)</span><br><span class="line">	&#125;)</span><br><span class="line">	r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><img src="2-gin%E8%B7%AF%E7%94%B1.assets/image-20200915193608644.png" alt="image-20200915193608644"></p>
<h1 id="六-上传多个文件"><a href="#六-上传多个文件" class="headerlink" title="六 上传多个文件"></a>六 上传多个文件</h1><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// gin的helloWorld</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	<span class="comment">// 1.创建路由</span></span><br><span class="line">	<span class="comment">// 默认使用了2个中间件Logger(), Recovery()</span></span><br><span class="line">	r := gin.Default()</span><br><span class="line">	<span class="comment">// 限制表单上传大小 8MB，默认为32MB</span></span><br><span class="line">	r.MaxMultipartMemory = <span class="number">8</span> &lt;&lt; <span class="number">20</span></span><br><span class="line">	r.POST(<span class="string">"/upload"</span>, <span class="function"><span class="keyword">func</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">		form, err := c.MultipartForm()</span><br><span class="line">		<span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">			c.String(http.StatusBadRequest, fmt.Sprintf(<span class="string">"get err %s"</span>, err.Error()))</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">// 获取所有图片</span></span><br><span class="line">		files := form.File[<span class="string">"files"</span>]</span><br><span class="line">		<span class="comment">// 遍历所有图片</span></span><br><span class="line">		<span class="keyword">for</span> _, file := <span class="keyword">range</span> files &#123;</span><br><span class="line">			<span class="comment">// 逐个存</span></span><br><span class="line">			<span class="keyword">if</span> err := c.SaveUploadedFile(file, file.Filename); err != <span class="literal">nil</span> &#123;</span><br><span class="line">				c.String(http.StatusBadRequest, fmt.Sprintf(<span class="string">"upload err %s"</span>, err.Error()))</span><br><span class="line">				<span class="keyword">return</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		c.String(<span class="number">200</span>, fmt.Sprintf(<span class="string">"upload ok %d files"</span>, <span class="built_in">len</span>(files)))</span><br><span class="line">	&#125;)</span><br><span class="line">	<span class="comment">//默认端口号是8080</span></span><br><span class="line">	r.Run(<span class="string">":8000"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="2-gin%E8%B7%AF%E7%94%B1.assets/image-20200915194131055.png" alt="image-20200915194131055"></p>
<h1 id="七-routes-group"><a href="#七-routes-group" class="headerlink" title="七 routes group"></a>七 routes group</h1><p>routes group是为了管理一些相同的URL</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"fmt"</span></span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// gin的helloWorld</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   <span class="comment">// 1.创建路由</span></span><br><span class="line">   r := gin.Default()</span><br><span class="line">   <span class="comment">// 路由组1 ，处理GET请求</span></span><br><span class="line">   v1 := r.Group(<span class="string">"/v1"</span>)</span><br><span class="line">   <span class="comment">// &#123;&#125; 是书写规范,不写也开业</span></span><br><span class="line">   &#123;</span><br><span class="line">      v1.GET(<span class="string">"/login"</span>, login)</span><br><span class="line">      v1.GET(<span class="string">"submit"</span>, submit) <span class="comment">//  /不写也可以</span></span><br><span class="line">   &#125; </span><br><span class="line">   v2 := r.Group(<span class="string">"/v2"</span>)</span><br><span class="line"></span><br><span class="line">   v2.POST(<span class="string">"/login"</span>, login)</span><br><span class="line">   v2.POST(<span class="string">"/submit"</span>, submit)</span><br><span class="line"></span><br><span class="line">   r.Run(<span class="string">":8000"</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">login</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   name := c.DefaultQuery(<span class="string">"name"</span>, <span class="string">"lqz"</span>)</span><br><span class="line">   c.String(<span class="number">200</span>, fmt.Sprintf(<span class="string">"hello %s\n"</span>, name))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">submit</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   name := c.DefaultQuery(<span class="string">"name"</span>, <span class="string">"egon"</span>)</span><br><span class="line">   c.String(<span class="number">200</span>, fmt.Sprintf(<span class="string">"hello %s\n"</span>, name))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">get请求：<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/v1/login</span><br><span class="line">get请求：<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/v1/submit</span><br><span class="line"></span><br><span class="line">post请求：<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/v1/login</span><br><span class="line">post请求：<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/v1/submit</span><br></pre></td></tr></table></figure>

<h1 id="八-路由拆分与注册"><a href="#八-路由拆分与注册" class="headerlink" title="八 路由拆分与注册"></a>八 路由拆分与注册</h1><h2 id="8-1-基本路由注册"><a href="#8-1-基本路由注册" class="headerlink" title="8.1 基本路由注册"></a>8.1 基本路由注册</h2><p>适用于路由比较少的简单项目</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line"></span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">helloHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"message"</span>: <span class="string">"Hello world!"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   r := gin.Default()</span><br><span class="line">   r.GET(<span class="string">"/hello"</span>, helloHandler)</span><br><span class="line">   r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="8-2-路由拆分成单独文件或包"><a href="#8-2-路由拆分成单独文件或包" class="headerlink" title="8.2 路由拆分成单独文件或包"></a>8.2 路由拆分成单独文件或包</h2><p>当项目的规模增大后就不太适合继续在项目的<code>main.go</code>文件中去实现路由注册相关逻辑了，我们会倾向于把路由部分的代码都拆分出来，形成一个单独的文件或包</p>
<h3 id="8-2-1-拆分到单独文件中"><a href="#8-2-1-拆分到单独文件中" class="headerlink" title="8.2.1 拆分到单独文件中"></a>8.2.1 拆分到单独文件中</h3><h4 id="routers-go"><a href="#routers-go" class="headerlink" title="routers.go"></a>routers.go</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line"></span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">helloHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"www.liuqingzheng.top"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">testHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"status"</span>:<span class="number">100</span>,</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"hello world"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">initRouter</span><span class="params">()</span> *<span class="title">gin</span>.<span class="title">Engine</span></span> &#123;</span><br><span class="line">   r := gin.Default()</span><br><span class="line">   r.GET(<span class="string">"/hello"</span>, helloHandler)</span><br><span class="line">   r.GET(<span class="string">"/test"</span>, testHandler)</span><br><span class="line">   <span class="keyword">return</span> r</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="main-go"><a href="#main-go" class="headerlink" title="main.go"></a>main.go</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   r := initRouter()</span><br><span class="line">   r.Run()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//访问</span></span><br><span class="line"><span class="comment">//http://127.0.0.1:8080/test</span></span><br><span class="line">http:<span class="comment">//127.0.0.1:8080/hello</span></span><br></pre></td></tr></table></figure>

<h4 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h4><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girqj29jfvj30e4062jv1.jpg" alt="image-20200915230640851" style="zoom:50%;" />

<h3 id="8-2-2-拆分到包中"><a href="#8-2-2-拆分到包中" class="headerlink" title="8.2.2 拆分到包中"></a>8.2.2 拆分到包中</h3><p><code>routers/routers.go</code>需要注意此时initRouter需要改成首字母大写：</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> routers</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line"></span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">helloHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"www.liuqingzheng.top"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">testHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"status"</span>:<span class="number">100</span>,</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"hello world"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">InitRouter</span><span class="params">()</span> *<span class="title">gin</span>.<span class="title">Engine</span></span> &#123;</span><br><span class="line">   r := gin.Default()</span><br><span class="line">   r.GET(<span class="string">"/hello"</span>, helloHandler)</span><br><span class="line">   r.GET(<span class="string">"/test"</span>, testHandler)</span><br><span class="line">   <span class="keyword">return</span> r</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="main-go-1"><a href="#main-go-1" class="headerlink" title="main.go"></a>main.go</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"gin_test/routers"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   r := routers.InitRouter()</span><br><span class="line">   r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="目录结构-1"><a href="#目录结构-1" class="headerlink" title="目录结构"></a>目录结构</h4><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girqobrerej30fm076aeu.jpg" alt="image-20200915231146634" style="zoom:50%;" />

<h2 id="8-3-路由拆分成多个文件"><a href="#8-3-路由拆分成多个文件" class="headerlink" title="8.3 路由拆分成多个文件"></a>8.3 路由拆分成多个文件</h2><p>单独的一个<code>routers</code>文件或包已经满足不了我们的需求了</p>
<h3 id="目录结构-2"><a href="#目录结构-2" class="headerlink" title="目录结构"></a>目录结构</h3><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girqyxnbznj30g6084q8m.jpg" alt="image-20200915232150667" style="zoom:50%;" />

<h3 id="routers-goods-go"><a href="#routers-goods-go" class="headerlink" title="routers/goods.go"></a>routers/goods.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> routers</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">LoadGoodsRouters</span><span class="params">(e *gin.Engine)</span></span>  &#123;</span><br><span class="line">   e.GET(<span class="string">"/good"</span>, goodHandler)</span><br><span class="line">   e.GET(<span class="string">"/goodlist"</span>, goodlistHandler)</span><br><span class="line">   e.GET(<span class="string">"/updategood"</span>, updategoodHandler)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">goodHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"www.liuqingzheng.top"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">goodlistHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"goodlistHandler"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">updategoodHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"updategoodHandler"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="routers-order-go"><a href="#routers-order-go" class="headerlink" title="routers/order.go"></a>routers/order.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> routers</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">LoadOrderRouters</span><span class="params">(e *gin.Engine)</span></span>  &#123;</span><br><span class="line">   e.GET(<span class="string">"/order"</span>, orderHandler)</span><br><span class="line">   e.GET(<span class="string">"/orderlist"</span>, orderlistHandler)</span><br><span class="line">   e.GET(<span class="string">"/updateorder"</span>, updateorderHandler)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">orderHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"www.liuqingzheng.top"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">orderlistHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"orderlistHandler"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">updateorderHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"updateorderHandler"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="routers-user-go"><a href="#routers-user-go" class="headerlink" title="routers/user.go"></a>routers/user.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> routers</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">LoadUserRouters</span><span class="params">(e *gin.Engine)</span></span>  &#123;</span><br><span class="line">   e.GET(<span class="string">"/user"</span>, userHandler)</span><br><span class="line">   e.GET(<span class="string">"/userlist"</span>, userlistHandler)</span><br><span class="line">   e.GET(<span class="string">"/updateuser"</span>, updateuserHandler)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">userHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.JSON(http.StatusOK, gin.H&#123;</span><br><span class="line">      <span class="string">"msg"</span>: <span class="string">"www.liuqingzheng.top"</span>,</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">userlistHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"userlistHandler"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">updateuserHandler</span><span class="params">(c *gin.Context)</span></span> &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"updateuserHandler"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="main-go-2"><a href="#main-go-2" class="headerlink" title="main.go"></a>main.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"gin_test/routers"</span></span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   r := gin.Default()</span><br><span class="line">   routers.LoadGoodsRouters(r)</span><br><span class="line">   routers.LoadOrderRouters(r)</span><br><span class="line">   routers.LoadUserRouters(r)</span><br><span class="line">   r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="8-4-路由拆分到不同的APP"><a href="#8-4-路由拆分到不同的APP" class="headerlink" title="8.4 路由拆分到不同的APP"></a>8.4 路由拆分到不同的APP</h2><p>有时候项目规模实在太大，那么我们就更倾向于把业务拆分的更详细一些，例如把不同的业务代码拆分成不同的APP。</p>
<p>因此我们在项目目录下单独定义一个<code>app</code>目录，用来存放我们不同业务线的代码文件，这样就很容易进行横向扩展。大致目录结构如下：</p>
<img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1girrege7zbj30ge0dgqco.jpg" alt="image-20200915233649087" style="zoom:50%;" />

<h3 id="app01-shop-handler-go"><a href="#app01-shop-handler-go" class="headerlink" title="app01/shop/handler.go"></a>app01/shop/handler.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> shop</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">shopHandler</span><span class="params">(c *gin.Context)</span></span>  &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"shopHandler"</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">shopInfoHandler</span><span class="params">(c *gin.Context)</span></span>  &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"shopInfoHandler"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="app01-shop-router-go"><a href="#app01-shop-router-go" class="headerlink" title="app01/shop/router.go"></a>app01/shop/router.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> shop</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Routers</span><span class="params">(e *gin.Engine)</span></span> &#123;</span><br><span class="line">   e.GET(<span class="string">"/shop"</span>, shopHandler)</span><br><span class="line">   e.GET(<span class="string">"/shopinfo"</span>, shopInfoHandler)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="app01-user-handler-go"><a href="#app01-user-handler-go" class="headerlink" title="app01/user/handler.go"></a>app01/user/handler.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> user</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line">   <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">userHandler</span><span class="params">(c *gin.Context)</span></span>  &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"userHandler"</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">userInfoHandler</span><span class="params">(c *gin.Context)</span></span>  &#123;</span><br><span class="line">   c.String(http.StatusOK, <span class="string">"userInfoHandler"</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="app01-user-router-go"><a href="#app01-user-router-go" class="headerlink" title="app01/user/router.go"></a>app01/user/router.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> user</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Routers</span><span class="params">(e *gin.Engine)</span></span> &#123;</span><br><span class="line">   e.GET(<span class="string">"/user"</span>, userHandler)</span><br><span class="line">   e.GET(<span class="string">"/userinfo"</span>, userInfoHandler)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="routers-routers-go"><a href="#routers-routers-go" class="headerlink" title="routers/routers.go"></a>routers/routers.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> routers</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"github.com/gin-gonic/gin"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> Option <span class="function"><span class="keyword">func</span><span class="params">(engine *gin.Engine)</span></span></span><br><span class="line"><span class="keyword">var</span> options=[]Option&#123;&#125;</span><br><span class="line"><span class="comment">//注册app的路由配置</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">Include</span><span class="params">(opts ...Option)</span></span>  &#123;</span><br><span class="line">   options=<span class="built_in">append</span>(options,opts...)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//初始化</span></span><br><span class="line"><span class="function"><span class="keyword">func</span>  <span class="title">Init</span><span class="params">()</span>*<span class="title">gin</span>.<span class="title">Engine</span></span>  &#123;</span><br><span class="line">   r:=gin.New()</span><br><span class="line">   <span class="keyword">for</span> _,opt:=<span class="keyword">range</span> options&#123;</span><br><span class="line">      opt(r)</span><br><span class="line">   &#125;</span><br><span class="line">   <span class="keyword">return</span> r</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="main-go-3"><a href="#main-go-3" class="headerlink" title="main.go"></a>main.go</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">   <span class="string">"gin_test/app01/shop"</span></span><br><span class="line">   <span class="string">"gin_test/app01/user"</span></span><br><span class="line">   <span class="string">"gin_test/routers"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">   routers.Include(shop.Routers,user.Routers)</span><br><span class="line">   r:=routers.Init()</span><br><span class="line">   r.Run()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/go/4-Go%E8%AF%AD%E8%A8%80%E6%A1%86%E6%9E%B6/4-1-gin%E6%A1%86%E6%9E%B6/1-gin%E6%A1%86%E6%9E%B6%E7%AE%80%E4%BB%8B/">
        <span class="nav-arrow">← </span>
        
          go/4-Go语言框架/4-1-gin框架/1-gin框架简介
        
      </a>
    
    
      <a class="nav-right" href="/go/4-Go%E8%AF%AD%E8%A8%80%E6%A1%86%E6%9E%B6/4-1-gin%E6%A1%86%E6%9E%B6/3-gin%E6%95%B0%E6%8D%AE%E8%A7%A3%E6%9E%90%E5%92%8C%E7%BB%91%E5%AE%9A/">
        
          go/4-Go语言框架/4-1-gin框架/3-gin数据解析和绑定
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#一-基本路由"><span class="toc-nav-text">一 基本路由</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#二-API参数"><span class="toc-nav-text">二 API参数</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#三-URL参数"><span class="toc-nav-text">三 URL参数</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#四-表单参数"><span class="toc-nav-text">四 表单参数</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#五-上传单个文件"><span class="toc-nav-text">五 上传单个文件</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#六-上传多个文件"><span class="toc-nav-text">六 上传多个文件</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#七-routes-group"><span class="toc-nav-text">七 routes group</span></a></li><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#八-路由拆分与注册"><span class="toc-nav-text">八 路由拆分与注册</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#8-1-基本路由注册"><span class="toc-nav-text">8.1 基本路由注册</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#8-2-路由拆分成单独文件或包"><span class="toc-nav-text">8.2 路由拆分成单独文件或包</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#8-2-1-拆分到单独文件中"><span class="toc-nav-text">8.2.1 拆分到单独文件中</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#routers-go"><span class="toc-nav-text">routers.go</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#main-go"><span class="toc-nav-text">main.go</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#目录结构"><span class="toc-nav-text">目录结构</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#8-2-2-拆分到包中"><span class="toc-nav-text">8.2.2 拆分到包中</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#main-go-1"><span class="toc-nav-text">main.go</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#目录结构-1"><span class="toc-nav-text">目录结构</span></a></li></ol></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#8-3-路由拆分成多个文件"><span class="toc-nav-text">8.3 路由拆分成多个文件</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#目录结构-2"><span class="toc-nav-text">目录结构</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#routers-goods-go"><span class="toc-nav-text">routers&#x2F;goods.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#routers-order-go"><span class="toc-nav-text">routers&#x2F;order.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#routers-user-go"><span class="toc-nav-text">routers&#x2F;user.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#main-go-2"><span class="toc-nav-text">main.go</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#8-4-路由拆分到不同的APP"><span class="toc-nav-text">8.4 路由拆分到不同的APP</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#app01-shop-handler-go"><span class="toc-nav-text">app01&#x2F;shop&#x2F;handler.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#app01-shop-router-go"><span class="toc-nav-text">app01&#x2F;shop&#x2F;router.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#app01-user-handler-go"><span class="toc-nav-text">app01&#x2F;user&#x2F;handler.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#app01-user-router-go"><span class="toc-nav-text">app01&#x2F;user&#x2F;router.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#routers-routers-go"><span class="toc-nav-text">routers&#x2F;routers.go</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#main-go-3"><span class="toc-nav-text">main.go</span></a></li></ol></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/go/4-Go语言框架/4-1-gin框架/2-gin路由/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "go/4-Go语言框架/4-1-gin框架/2-gin路由",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>